+-2sigma.Rmdのロジックを良くしたい
やりたいこと
mm/ddのリターンが過去〇〇日のリターンの-2σ以上なのか以下なのか判別したい
# rm(tmp)
# rm(tmp1)
# rm(tmp2)
# rm(touraku2)
#過去1週間分を評価する
#結果テーブル準備
# tmp2 <- data.frame('date'=2017-01-01 , 'close'=999 , 'ticker'='aaa' , 'rtn'=0.99 ,
# 'sigma'=0.99 , 'buysign'='buy')[-1,]
# tmp2 <- as.Date(tmp2[,1])
# touraku2 <- data.frame('date'=2017-01-01 , 'close'=999 , 'ticker'='aaa' , 'rtn'=0.99 ,
# 'sigma'=0.99 , 'buysign'='buy')[-1,]
# touraku2[,1] <- as.Date(touraku2[,1])
tmp2 <- dailyrtn
tmp2 <- transform(tmp2, sigma=0)
tmp2 <- transform(tmp2, buysign='--')
tmp2[,1] <- ymd(tmp2[,1])
tmp2[,6] <- as.character(tmp2[,6])
tmp2 <- tmp2[-1,]
touraku2 <- head(dailyrtn,1)
touraku2 <- transform(touraku2, sigma=0)
touraku2 <- transform(touraku2, buysign='--')
touraku2[,1] <- ymd(touraku2[,1])
touraku2[,6] <- as.character(touraku2[,6])
touraku2 <- touraku2[-1,]
for (i in 1:length(ticker_tmp)){
tmp <- subset(dailyrtn, ticker == ticker_tmp[i])
#列追加
tmp <- transform(tmp, sigma=0)
tmp <- transform(tmp, buysign='--')
tmp$date <- ymd(tmp$date)
tmp[,6] <- as.character(tmp[,6])
tmp <- tmp[-1,]
#1週間前の位置 1週間前から現在までのcloseを評価するの意
tarRow1 <- nrow(tmp) - 6
for (k in tarRow1:nrow(tmp)) {
#過去300日分のリターンの-2σ計算
#tarRow2 <- k - 300
#過去300日分の抽出
#tmp1 <- subset(tmp,
# as.numeric(row.names(tmp)) >= tarRow2
# & row.names(tmp) <= tarRow1 )
tmp1 <- tmp[1:tarRow1,]
tmp1 <- tail(tmp1, 300)
#-2σ計算
under2sigma <- quantile(tmp1$rtn,c(0.02275))
#-2σの追記
tmp[k, 5] <- under2sigma
#リターンとunder2sigmaを比較してbuyサインを立てる
if (tmp[k, 4] < tmp[k, 5]) {
tmp[k, 6] <- 'buy'
} else {
tmp[k, 6] <- '-'
}
}
#結果をテーブルへ
tmp2 <- rbind(tmp2, tmp)
touraku2 <- subset(tmp2, buysign=='buy')
touraku2$ticker <- as.factor(touraku2$ticker)
#1週間以内でフィルタ
touraku2 <- touraku2 %>%
filter(date >= Sys.Date()-7)
}
datatable(touraku2,
filter = 'top',
style = 'bootstrap', class = 'table-bordered table-condensed',
extensions = 'ColReorder',
options = list(dom = 'Rlfrtip')
)
#write.table(touraku2,'touraku2.txt',sep="\t",quote=F,row.names = F)
ticker_bakusagari <- as.character(t(touraku2$ticker))
#購入済で下がった銘柄は?
ticker_kaimashikoho <- intersect(ticker_bakusagari, kounyu)
#購入済でない銘柄は?
#setdiff(a,b)はaにあってbにないものを抽出する
ticker_shinkikoho <- setdiff(ticker_bakusagari, kounyu)
# tmp <- subset(kokunaiETF, kokunaiETF$ticker %in% ticker_bakusagari)
# datatable(tmp,
# filter = 'top',
# style = 'bootstrap', class = 'table-bordered table-condensed',
# extensions = 'ColReorder',
# options = list(dom = 'Rlfrtip')
# )
購入済で下がった銘柄
2510
新規購入候補の銘柄
1305, 1308, 1348, 1473, 1475, 2557, 1320, 1321, 1330, 1329, 1346, 1578, 1397, 1591, 1592, 1593, 1364, 1551, 1311, 1312, 1493, 1621, 1622, 1623, 1625, 1627, 1629, 1630, 1632, 1698, 1577, 1586, 2523, 1477, 1480, 1484, 2529, 1499, 1568, 1569, 1457, 1356, 1368, 1570, 1579, 1358, 1365, 1458, 1465, 1466, 1469, 1472, 1343, 1345, 1597, 1595, 1398, 1476, 1488, 2552, 2555, 2556, 2517, 2527, 2528, 1555, 1495, 1659, 1660, 2515, 1313, 1584, 2530, 1678, 1559, 1560, 1390, 1385, 1388, 1391, 1392, 1679, 1546, 1547, 2521, 1545, 1323, 1324, 1325, 1394, 1550, 2513, 2514, 1681, 2520, 1554, 1657, 1658, 2522, 1349, 1677, 2511, 2512, 1566, 2519, 1482, 1486, 1487, 1496, 1497, 1656, 2554, 1328, 1326, 1672, 1540, 1683, 1541, 1682, 1673, 1542, 1543, 1671, 1699, 1690, 1696, 2031, 2033, 2035, 2036, 2037, 2038, 2040, 2041, 2043, 2044, 2045, 2046, 2066, 2068, 2069, 2979, 2972, 2971, 3493, 3492, 3488, 3487, 3481, 3478, 3476, 3473, 3472, 3471, 3470, 3468, 3466, 3463, 3462, 3459, 3455, 3453, 3309, 3451, 3308, 3298, 3296, 3295, 3292, 3290, 3287, 3283, 3282, 3281, 3279, 3278, 8979, 3269, 3249, 3234, 3227, 3226, 8963, 8987, 8986, 8985, 8984, 8977, 8976, 8975, 8972, 8968, 8967, 8966, 8964, 8961, 8960, 8958, 8957, 8956, 8955, 8954, 8953, 8952, 8951
#国内
if (length(intersect(touraku2$ticker, kokunaiETF)) >= 1) {
g <- ggplot(NULL)
g <- g + geom_line(data = subset(daily1,
ticker %in% intersect(touraku2$ticker, kokunaiETF) &
year(date) >= 2017),
aes(x=date, y=close, colour=ticker))
g <- g + geom_point(data = subset(touraku2,
ticker%in% intersect(touraku2$ticker, kokunaiETF) &
year(date) >= 2017),
aes(x=date, y=close, colour=ticker))
ggplotly(g)
} else {
tmp <- 0
}
#海外
if (length(intersect(touraku2$ticker, kaigai)) >= 1) {
g <- ggplot(NULL)
g <- g + geom_line(data = subset(daily1,
ticker %in% intersect(touraku2$ticker, kaigai) &
year(date) >= 2017),
aes(x=date, y=close, colour=ticker))
g <- g + geom_point(data = subset(touraku2,
ticker%in% intersect(touraku2$ticker, kaigai) &
year(date) >= 2017),
aes(x=date, y=close, colour=ticker))
ggplotly(g)
} else {
tmp <- 0
}
ggplotly(
ggplot(subset(monthlyrtn, ticker %in% ticker_bakusagari),
aes(x = return_monthly, colour = ticker)
) + geom_density()
)